package com.example.advice.SqlTypeHandler;

import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * LocalDateTime自定义handler
 * 只处理get，将获取到的结果+1s
 * @author Destinycc
 */
public class LocalDateTimeHandler extends LocalDateTimeTypeHandler {
    @Override
    public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
        LocalDateTime time = (LocalDateTime)rs.getObject(columnName, LocalDateTime.class);
        if (Objects.isNull(time)){
            return null;
        }
        time = time.plusSeconds(1);
        return time;
    }

    @Override
    public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        LocalDateTime time = (LocalDateTime)rs.getObject(columnIndex, LocalDateTime.class);
        if (Objects.isNull(time)){
            return null;
        }
        time = time.plusSeconds(1);
        return time;
    }

    @Override
    public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        LocalDateTime time = (LocalDateTime)cs.getObject(columnIndex, LocalDateTime.class);
        if (Objects.isNull(time)){
            return null;
        }
        time = time.plusSeconds(1);
        return time;
    }

}
